package com.yanqu.road.server.gameplayer.module.rebate;


import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.rebate.ChinaUserRebate;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.CardModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.SecretVolumeModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.config.MallMgr;
import com.yanqu.road.server.manger.player.WorldMessageMgr;
import com.yanqu.road.server.manger.rebate.ChinaRebateMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

public class ChinaRebateModule extends GeneralModule {

    //上线要改成对应返利区服和对应返利时间
    //返利条件
    private long serverId = 31100001L;

    private long startTime = 1635955200000L;

    private long endTime = startTime + 35L*24*60*60*1000;


    private ChinaUserRebate chinaUserRebate;

    public ChinaRebateModule(GamePlayer player) {
        super(player);
    }

    //返利条件修改 --task=1002483 --user=邱佳祥 国服删档返利功能调整 https://www.tapd.cn/59651728/s/1050906
    private synchronized boolean isRebate(){
        long localServerId = GameServer.getInstance().getServerId();
        if(localServerId != serverId && localServerId != 30300001 && localServerId != 30300002){
            return false;
        }
        if(System.currentTimeMillis() < startTime){
            return false;
        }
        if(System.currentTimeMillis() > endTime){
            return false;
        }
        return true;
    }

    public synchronized void rebateVipExp(){
        if (isRebate() && chinaUserRebate != null && !chinaUserRebate.isGetVipExp()) {
            // 还未发送邮件 创建奖励邮件
            UserInfo userInfo = player.getUserInfo();

            long IngotsNum = chinaUserRebate.getVipExp() * 15;

            long vipExp = chinaUserRebate.getVipExp();

            Property reward = new Property();

            reward.addProperty(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(IngotsNum));
            reward.addProperty(GameConfig.GAME_MONEY_VIPEXP, BigInteger.valueOf(vipExp));

            chinaUserRebate.setGetVipExp(true);
            chinaUserRebate.setRebateUserId(userInfo.getUserId());
            chinaUserRebate.setRebateUserName(userInfo.getNickName());
            chinaUserRebate.setUpdateTime(System.currentTimeMillis());

            String title = MultipleLanguageMgr.getContent(MailManager.REBATE_TITLE, userInfo.getLanguage());
            String content = MultipleLanguageMgr.getContent(MailManager.REBATE_CONTENT, userInfo.getLanguage());
            MailManager.sendMail(userInfo.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), content, title);
            log.error("发送充值返利邮件成功，userId: {}，昵称: {}，vipExp : {},返还元宝：{}，时间: {}",
                    userInfo.getUserId(), userInfo.getNickName(), vipExp, IngotsNum, DateHelper.getCurrentDate());
        }
    }

    public synchronized void rebateCard(){
        if (isRebate() && chinaUserRebate != null) {
            if(chinaUserRebate.isBuyYear() && !chinaUserRebate.isGetYear()){
                player.getModule(CardModule.class).addCard(eCardType.YearCard.getValue(), true,
                        GameConfig.LIFE_CARD_MALL_ID, null);
                chinaUserRebate.setGetYear(true);
                chinaUserRebate.setUpdateTime(System.currentTimeMillis());
                log.info("返利特权卡年卡: {}，时间: {}", player.getUserId(), DateHelper.getCurrentDate());
            }
            if(chinaUserRebate.isBuyMonth() && !chinaUserRebate.isGetMonth()){
                player.getModule(CardModule.class).addCard(eCardType.MonthCard.getValue(), true,
                        GameConfig.WEEK_CARD_MALL_ID, null);
                chinaUserRebate.setGetMonth(true);
                chinaUserRebate.setUpdateTime(System.currentTimeMillis());
                log.info("返利特权卡月卡: {}，时间: {}", player.getUserId(), DateHelper.getCurrentDate());
            }
        }
    }

    public synchronized void rebateSecretVolume(){
        if (isRebate() && chinaUserRebate != null) {
            if(chinaUserRebate.isBuySecret() && !chinaUserRebate.isGetSecret()){
                MallInfo mallInfo = MallMgr.getMallInfoByMallId(GameConfig.SECRET_VOLUME_MALL_ID);
                player.getModule(SecretVolumeModule.class).buySecretVolume();
                player.getModule(CurrencyModule.class).addCurrency(mallInfo.getReward(), eLogMoneyType.Recharge, eLogMoneyType.ReChargeGet);
                chinaUserRebate.setGetSecret(true);
                chinaUserRebate.setUpdateTime(System.currentTimeMillis());
                log.info("返利财神密卷: {}，时间: {}", player.getUserId(), DateHelper.getCurrentDate());
            }
        }
    }

    public synchronized void rebatePatronFund(){
        if (isRebate() && chinaUserRebate != null) {
            if(chinaUserRebate.isBuyFund() && !chinaUserRebate.isGetFund()){
                MallInfo mallInfo = MallMgr.getMallInfoByMallId(GameConfig.ACTIVITY_PATRONS_FUND);
                player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.PatronsFund, 1);
                player.getModule(CurrencyModule.class).addCurrency(mallInfo.getReward(), eLogMoneyType.System, eLogMoneyType.SystemSend);
                //门客基金世界展示
                WorldMessageMgr.worldNotify(player, eMessageType.BuyPatronsFund.getValue());
                chinaUserRebate.setGetFund(true);
                chinaUserRebate.setUpdateTime(System.currentTimeMillis());
                log.info("返利门客基金: {}，时间: {}", player.getUserId(), DateHelper.getCurrentDate());
            }
        }
    }

    @Override
    public boolean loadData() {
        UserInfo userInfo = player.getUserInfo();
        if (isRebate()) {
            chinaUserRebate = ChinaRebateMgr.getChinaUserRebate(userInfo.getOpenId());
        } else {
            chinaUserRebate = null;
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public void loginSendMsg() {

    }

}
